package net.suncaper.demo.controller;

import net.suncaper.demo.domain.*;
import net.suncaper.demo.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/question")
public class Question_controller {
    @Autowired
    private QuestionService questionService;
    private int select;
    private String UserId;
    @RequestMapping("/set")
    public String SetAdministratorId(@RequestParam(value = "UserId", required = false) String UserId,@RequestParam(value = "select", required = false) int select) {
        this.UserId=UserId;
        this.select=select;
        if(select==1){
            return "redirect:/reply/set?UserId="+UserId+"&select=1";}
        if(select==2){
            return "redirect:/reply/set?UserId="+UserId+"&select=2";}
        else{
            return "redirect:/reply/set?UserId="+UserId+"&select=3";}
    }
    @GetMapping("/add")
    public String saveInformation(Model model,Model mode0) {
        model.addAttribute("question", new GiveQuestion());
        mode0.addAttribute("UserId",UserId);
        return "pages/question/WriteQuestion.html";
    }

    @PostMapping("/add")
    public String saveInformation(GiveQuestion question) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        question.setDate(df.format(new Date()));
        question.setIsEnd((short)1);
        question.setTeacherId(UserId);
        questionService.saveGiveQuestion(question);
        return "redirect:/question";
    }
    @RequestMapping("/report")
    public String report(Model model,Model mode0){
        List <Report> report=questionService.findReport();
        List<GiveQuestion> givequestion=questionService.findGiveQuestion();
        List<SelectQuestion> selectquestion=questionService.findSelectQuestion();
        List<Report> report1=new ArrayList<Report>();
        for(int i=0;i<givequestion.size();i++){
            if(givequestion.get(i).getTeacherId().equals(UserId)) {
                for(int j=0;j<selectquestion.size();j++){
                    if(selectquestion.get(j).getQuestionId().equals(givequestion.get(i).getQuestionId())){
                        for(int m=0;m<report.size();m++){
                            if(report.get(m).getSelectQuestionId().equals(selectquestion.get(j).getSelectQuestionId())){
                                report.get(m).setStudentId(questionService.findstudentById(selectquestion.get(j).getStudentId()).getName());
                                report1.add(report.get(m));
                            }
                        }
                    }

                }
            }
        }
        model.addAttribute("report", report1);
        mode0.addAttribute("UserId",UserId);
        return "/pages/report/report.html";
    }
    @GetMapping("/writereport")
    public String wtitereport(Model model,Model mode0) {
        List<SelectQuestion> selectquestion=questionService.findSelectQuestion();
        GiveQuestion giveQuestion=new GiveQuestion();
        List <Report> report=questionService.findReport();
        for(int i=0;i<report.size();i++){
            if(report.get(i).getStudentId().equals(UserId)) {
                return "redirect:/question/myreport";
            }
        }

        for(int i=0;i<selectquestion.size();i++){
            if(selectquestion.get(i).getStudentId().equals(UserId)) {
                giveQuestion=questionService.findGiveQuestionById(selectquestion.get(i).getQuestionId());
                break;
            }
        }
        model.addAttribute("giveQuestion", giveQuestion);
        mode0.addAttribute("UserId",UserId);
        return "/pages/report/writereport.html";
    }
    @PostMapping("/savereport")
    public String savereport(Report report) {
        List<SelectQuestion> selectquestion=questionService.findSelectQuestion();
        for(int i=0;i<selectquestion.size();i++){
            if(selectquestion.get(i).getStudentId().equals(UserId)) {
                report.setSelectQuestionId(selectquestion.get(i).getSelectQuestionId());
                break;
            }
        }
        report.setStudentId(UserId);
        questionService.saveReport(report);
        return "redirect:/question/myreport";
    }
    @GetMapping("/review")
    public String review(Model model,Model model0,Model mode0) {
        BigDecimal reportId=new BigDecimal(0);
        List <Review> review=questionService.FindReview();
        List <Report> report=questionService.findReport();
        for(int i=0;i<report.size();i++){
            if(report.get(i).getStudentId().equals(UserId)) {
                 reportId=report.get(i).getReportId();
                model0.addAttribute("report", report.get(i));
                break;
            }
        }
        for(int i=0;i<review.size();i++) {
            if (review.get(i).getReportId().equals(reportId)) {
                model.addAttribute("review", review.get(i));
                break;
            }
        }
        mode0.addAttribute("UserId",UserId);
        return "pages/report/review.html";
    }
    @RequestMapping("/writereview")
    public String writereview(Model model,Model model0,Model mode0,@RequestParam(value = "name", required = false) BigDecimal reportId) {
        Report report=questionService.findReportById(reportId);
        List<Review> reviews=questionService.FindReview();
        String content="";
        for(int i=0;i<reviews.size();i++){
            if(reviews.get(i).getReportId().equals(reportId)){
                content=reviews.get(i).getContent();
                break;
            }
        }
        model0.addAttribute("content",content);
        mode0.addAttribute("UserId",UserId);
        model.addAttribute("report", report);
        return "pages/report/writereview.html";
    }
    @PostMapping("/savereview")
    public String saveReview(Review review,@RequestParam(value = "name", required = false) BigDecimal reportId) {
          review.setReportId(reportId);
          review.setTeacherId(UserId);
          questionService.saveReview(review);
       return "redirect:/question/report";
    }
    @GetMapping("/myreport")
    public String myreport(Model model,Model model0) {
        List <Report> report=questionService.findReport();
        for(int i=0;i<report.size();i++){
            if(report.get(i).getStudentId().equals(UserId)) {
                model0.addAttribute("report", report.get(i));
                break;
            }
        }
        model.addAttribute("UserId",UserId);
        return "pages/report/myreport.html";
    }
    @RequestMapping("/search")
    public String searchInformation(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal questionId) {
        GiveQuestion question = questionService.findGiveQuestionById(questionId);
        model.addAttribute("question", question);
        mode0.addAttribute("UserId",UserId);
        return "pages/question/showquestion.html";
    }
    @RequestMapping("/title")
    public String userPage1(Model model,Model mode0,@RequestParam(value = "title", required = false) String title) {
        List<GiveQuestion> question1 = questionService.findGiveQuestion();
        List<GiveQuestion> question2 = new ArrayList<GiveQuestion>();
        for(int i=0;i<question1.size();i++){
            if(question1.get(i).getTitle().indexOf(title) != -1) {
                question2.add(question1.get(i));
            }
        }
        model.addAttribute("question", question2);
        mode0.addAttribute("UserId",UserId);
        if(select==1) {
            return "pages/question/Administrator.html";
        }
        else if(select==2){
            return "pages/question/Teacher.html";
        }
        else{
            return "pages/question/Student.html";
        }
    }
    @RequestMapping("/updatereport")
    public String updatereport(Model model,Model mode0) {
        List <Report> report=questionService.findReport();
        for(int i=0;i<report.size();i++){
            if(report.get(i).getStudentId().equals(UserId)) {
                model.addAttribute("report", report.get(i));
                break;
            }
        }
        mode0.addAttribute("UserId",UserId);
        return "pages/report/updatereport.html";

    }
    @PostMapping("/updatereport")
    public String UPdatereport(Report report,@RequestParam(value = "name", required = false) BigDecimal reportId) {
        Report report1=questionService.findReportById(reportId);
        report.setReportId(reportId);
        report.setStudentId(report1.getStudentId());
        report.setSelectQuestionId(report1.getSelectQuestionId());
       questionService.updateReport(report);
        return "redirect:/question/myreport";
    }
    @RequestMapping("/select")
    public String selectQuestion(Model mode0){
        mode0.addAttribute("UserId",UserId);
        return "pages/qusetion/Student.html";
    }
    @GetMapping("/select")
    public String saveselect(SelectQuestion selectQuestion,@RequestParam(value = "name", required = false) BigDecimal questionId){
        selectQuestion.setSelectQuestionId(questionId);
        selectQuestion.setStudentId(UserId);
        selectQuestion.setQuestionId(questionId);
        short a=1;
        selectQuestion.setIsEnd(a);
        selectQuestion.setBeginApply(a);
        selectQuestion.setEndApply(a);
        questionService.saveSelect(selectQuestion);
        return "redirect:/question/myquestion";
    }
    @RequestMapping("/myquestion")
    public String myquestion(Model model,Model mode0){
        List<SelectQuestion> questions=questionService.findSelectQuestion();
        for (int i=0;i<questions.size();i++) {
            if (questions.get(i).getStudentId().equals(UserId)) {
                BigDecimal questionId = questions.get(i).getQuestionId();
                GiveQuestion question = questionService.selectquestionByquestionId(questionId);
                model.addAttribute("question",question);
                mode0.addAttribute("UserId",UserId);
                return "pages/question/myquestion.html";
            }
        }
        return "redirect:/question";

    }
    @RequestMapping("/delete")
    public String deleteQuestion(Model model,@RequestParam(value = "name", required = false) BigDecimal questionId) {
        questionService.deleteQuestionById(questionId);
        return "redirect:/question/manager";
    }
    @GetMapping("update")
    public String updateInformation(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal questionId) {
        GiveQuestion question = questionService.findQuestionById(questionId);
        model.addAttribute("question", question);
        mode0.addAttribute("UserId",UserId);
        return "/pages/question/updatequestion.html";
    }
    @PostMapping("/update")
    public String UPdateInformation(GiveQuestion question,@RequestParam(value = "name", required = false) BigDecimal questionId) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        question.setDate(df.format(new Date()));
        question.setTeacherId(UserId);
        question.setQuestionId(questionId);
        short a=1;
        question.setIsEnd(a);
        questionService.updateQuestion(question);
        return "redirect:/question/manager";
    }
    @RequestMapping("/manager")
    public String manager(Model model,Model mode0){

        if(select==1) {
            List<GiveQuestion> question = questionService.findGiveQuestion();
            model.addAttribute("question",question);
            return "pages/question/AdministratorManager.html";
        }
        else if(select==2){
            List<GiveQuestion> question0 = questionService.findGiveQuestion();
        List<GiveQuestion> question=new ArrayList<GiveQuestion>();
        for(int i=0;i<question0.size();i++){
            if (question0.get(i).getTeacherId().equals(UserId)){
                question.add(question0.get(i));
                question.get(i).setTeacherId(questionService.findTeachername(question.get(i).getTeacherId()).getName());
            }
        }
            mode0.addAttribute("UserId",UserId);
            model.addAttribute("question",question);
            return "pages/question/TeacherManager.html";
        }
        else{
            return "pages/question/StudentManager.html";
        }
    }
    @RequestMapping("/checkquestion")
    public String checkquestion(Model model,Model model0,Model model2){
        List<GiveQuestion> givequestion=questionService.findGiveQuestion();
        List<SelectQuestion> selectquestion=questionService.findSelectQuestion();
        List<Student> student1=questionService.findStudent();
        List<Student> student=new ArrayList<Student>();
        List<String>  question=new ArrayList<String>();
        for (int i=0;i<givequestion.size();i++) {
            if (givequestion.get(i).getTeacherId().equals(UserId)){
                for (int k=0;k<selectquestion.size();k++){
                    if (givequestion.get(i).getQuestionId().equals(selectquestion.get(k).getQuestionId())){
                        for (int j=0;j<student1.size();j++){
                            if (selectquestion.get(k).getStudentId().equals(student1.get(j).getStudentId())){
                                student.add(student1.get(j));
                                question.add(givequestion.get(i).getTitle());
                            }
                        }

                    }
                }
            }
        }
        model.addAttribute("student",student);
        model0.addAttribute("UserId",UserId);
        model2.addAttribute("question",question);
        return "/pages/question/Checkquestion.html";

        }

    @RequestMapping
    public String userPage(Model model,Model mode0,Model model2) {
        List<GiveQuestion> question = questionService.findGiveQuestion();
        List<Teacher> teachername=new ArrayList<Teacher>();
        for(int i=0;i<question.size();i++){
            question.get(i).setTeacherId(questionService.findTeachername(question.get(i).getTeacherId()).getName());
        }

        mode0.addAttribute("UserId",UserId);
        model.addAttribute("question",question);
//        return "pages/information/information.html";
        if(select==1) {
            return "pages/question/Administrator.html";
        }
        else if(select==2){
            return "pages/question/Teacher.html";
        }
        else{
            return "pages/question/Student.html";
        }
    }
}
